package com.woniu.smart.security;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: TODO
 * @Author: WangHeng
 * @Date: 2022/10/24 17:40
 */
public class LoginFilter extends AbstractAuthenticationProcessingFilter {
    //定义过滤器拦截的请求映射
    private final static String matchUrl = "/admins/login";

    public LoginFilter() {
        super(new AntPathRequestMatcher(matchUrl));
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        //得到账户和密码信息
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(request.getParameter("empTel"), request.getParameter("empPawd"));
        //开始执行认证框架
        return this.getAuthenticationManager().authenticate(authenticationToken);
    }
}
